package noname;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

class Description {

    private String id2;
    private String name;
    public Description(String id, String name) {
	super();
	this.id2 = id;
	this.name = name;
    }

    public String getId() {
	return id2;
    }

    public void setId(String id) {
	this.id2 = id;
    }
    
    public String getName() {
	return this.name;
    }

    public void setName(String name) {
	this.name = name;
    }

}

public class Friend {

    private String name;
    private String id;
    List aa = new ArrayList<Description>();

    public void setName(String name) {
	this.name = name;
    }

    public String getName() {
	return this.name;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getId() {
	return this.id;
    }

    public void setAa() {

	this.aa.add("a");

    }

    public List getAa() {
	return this.aa;
    }

    public String friend() throws ClassNotFoundException, SQLException {
	Friends friend = new Friends();
	ArrayList<Friend> fr = new ArrayList<Friend>();
	ArrayList<String> l = new ArrayList<String>();
	String q_friend = "select * from friends where id_user = " + getId();
	String q_user = "select * from users where id = ?";
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();
	Statement st = conn.createStatement();
	PreparedStatement ps;
	st.executeQuery(q_friend);
	ResultSet rs = st.getResultSet();

	ResultSet rs2;

	while (rs.next()) {

	    String id_user = rs.getString(3);
	    ps = conn.prepareStatement(q_user);
	    ps.setString(1, id_user);
	    ps.execute();
	    rs2 = ps.getResultSet();
	    if (rs2.next()) {
		aa.add(new Description(rs2.getString(1), rs2.getString(4)));
	    }
	}

	return "success";
    }

    public String add() throws ClassNotFoundException, SQLException {

	String search = "select * from users where login = ?";
	String insert = "insert into friends (id_user, id_friend) values (?,?)";

	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();

	String Name = getName();
	String Id = getId();

	PreparedStatement ps;
	ps = conn.prepareStatement(search);
	ps.setString(1, name);

	ps.execute();
	ResultSet rs = ps.getResultSet();
	if (rs.next()) {
	    String id_friend = rs.getString(1);
	    ps = conn.prepareStatement(insert);
	    ps.setString(1, id);
	    ps.setString(2, id_friend);
	    ps.execute();

	}

	return "success";
    }
}
